<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="rAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column type="index" label="序号" width="120" />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button size="small" type="success" @click="hAsyncRole(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="rEdit(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="rDel(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="pageParams.page"
                :page-sizes="[2, 3, 4, 5]"
                :page-size="pageParams.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 新增 编辑角色Dialog -->
      <el-dialog
        :title="isEdit?'编辑角色':'新增角色'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        @close="resetForm"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 分配权限Dialog -->
      <el-dialog title="分配权限" :visible.sync="showPerDialog">
        <AssignPer v-if="showPerDialog" :role-id="roleId" @close="showPerDialog=false" />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import AssignPer from './assignPermission.vue'
import { getRole, delRole, addRole, getRoleById, updateRole } from '@/api/settings'
export default {
  components: {
    AssignPer
  },
  data() {
    return {
      roleId: '',
      showPerDialog: false,
      isEdit: false,
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {},
      list: [],
      pageParams: {
        page: 1,
        pagesize: 2
      },
      total: 0
    }
  },
  created() {
    this.loadRole()
  },
  methods: {
    hAsyncRole(id) {
      this.roleId = id
      this.showPerDialog = true
    },
    // 重置表单
    resetForm() {
      this.$refs.roleForm.resetFields()
    },
    hSubmit() {
      this.isEdit ? this.doEdit() : this.doAdd()
    },
    rAdd() {
      this.showDialog = true
      this.isEdit = false
    },
    rEdit(id) {
      this.showDialog = true
      this.isEdit = true
      this.loadRoleById(id)
    },
    // 添加角色
    async doAdd() {
      const res = await addRole(this.roleForm)
      if (res.code !== 10000) return
      this.$message.success(res.message)
      // 需要获取最大的页码值
      this.total++
      // 将page修改为最大的页码值
      this.pageParams.page = Math.ceil(this.total / this.pageParams.pagesize)
      this.loadRole()
      this.showDialog = false
    },
    // 修改角色
    async doEdit() {
      const res = await updateRole(this.roleForm)
      console.log(res)
      if (res.code !== 10000) return
      this.$message.success(res.message)
      this.loadRole()
      this.showDialog = false
    },
    // 根据id获取角色信息
    async loadRoleById(id) {
      const res = await getRoleById(id)
      this.roleForm = res.data
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageParams.pagesize = val
      this.loadRole()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.pageParams.page = val
      this.loadRole()
    },
    // 删除角色
    rDel(id) {
      this.$confirm('你确定要删除这条数据吗?').then(async() => {
        const res = await delRole(id)
        if (res.code !== 10000) return
        this.$message.success(res.message)
        if (this.list.length === 1) {
          this.pageParams.page--
          if (this.pageParams.page <= 0) {
            this.pageParams.page = 1
          }
        }
        this.loadRole()
      }).catch(error => console.log(error))
    },
    // 获取所有角色列表
    async loadRole() {
      const res = await getRole(this.pageParams)
      console.log(res)
      this.list = res.data.rows
      this.total = res.data.total
    }
  }
}
</script>
